This tutorial contains information how to create and develop <i>JForex</i> strategies. The tutorial starts with a simple strategy which only prints a message, then proceeds to trading strategy which with each section gets more evolved as we add historical data, indicator and chart usage. Although all of the given strategies can be developed within the JForex platform, please consider the <a href="#Use_in_NetBeans/Strategy_development_in_NetBeans"> usage of an Integrated Development Environment (IDE)</a> for strategy development.<br/><div class="dw_article_headers_list"><div class="dw_article_header_h1"><a href="#Strategy_Tutorial/Simple_Strategy">Simple Strategy</a></div><div class="dw_article_header_h2"><a href="#Strategy_Tutorial/Modify_onStart">Modify onStart</a></div><div class="dw_article_header_h2"><a href="#Strategy_Tutorial/Run_the_strategy">Run the strategy</a></div><div class="dw_article_header_h1"><a href="#Strategy_Tutorial/Make_Simple_Trade">Make Simple Trade</a></div><div class="dw_article_header_h2"><a href="#Strategy_Tutorial/Define_Parameters">Define Parameters</a></div><div class="dw_article_header_h2"><a href="#Strategy_Tutorial/Getting_Bars_and_Ticks">Getting Bars and Ticks</a></div><div class="dw_article_header_h2"><a href="#Strategy_Tutorial/Make_a_Trade">Make a Trade</a></div><div class="dw_article_header_h1"><a href="#Strategy_Tutorial/Trade_According_to_Last_Completed_Bar">Trade According to Last Completed Bar</a></div><div class="dw_article_header_h2"><a href="#Strategy_Tutorial/Create_the_Logic_in_the_onBar_method">Create the Logic in the onBar method</a></div><div class="dw_article_header_h2"><a href="#Strategy_Tutorial/Log_an_Order">Log an Order</a></div><div class="dw_article_header_h1"><a href="#Strategy_Tutorial/Trade_According_to_SMA_Trend">Trade According to SMA Trend</a></div><div class="dw_article_header_h1"><a href="#Strategy_Tutorial/Representing_Events_on_Chart">Representing Events on Chart</a></div><div class="dw_article_header_h1"><a href="#Strategy_Tutorial/Add_Stop_Loss_and_Take_Profit">Add Stop Loss and Take Profit</a></div><div class="dw_article_header_h2"><a href="#Strategy_Tutorial/Break_Even_in_onTick">Break Even in onTick</a></div><div class="dw_article_header_h2"><a href="#Strategy_Tutorial/Trade_According_to_SMA">Trade According to SMA</a></div><div class="dw_article_header_h2"><a href="#Strategy_Tutorial/Adding_an_Indicator_to_the_Chart">Adding an Indicator to the Chart</a></div><div class="dw_article_header_h2"><a href="#Strategy_Tutorial/Plotting_a_Break-even_Triangle_on_the_Chart">Plotting a Break-even Triangle on the Chart</a></div><div class="dw_article_header_h2"><a href="#Strategy_Tutorial/Testing_the_Strategy">Testing the Strategy</a></div><div class="dw_article_header_h1"><a href="#Strategy_Tutorial/Using_Data_Feed">Using Data Feed</a></div><div class="dw_article_header_h2"><a href="#Strategy_Tutorial/Declaration_of_a_Feed_Type">Declaration of a Feed Type </a></div><div class="dw_article_header_h2"><a href="#Strategy_Tutorial/Subscription_to_a_Feed">Subscription to a Feed</a></div><div class="dw_article_header_h2"><a href="#Strategy_Tutorial/Implementation_of_a_IFeedListener_Interface">Implementation of a IFeedListener Interface</a></div><div class="dw_article_header_h2"><a href="#Strategy_Tutorial/Modification_of_the_Chart-checking_Method">Modification of the Chart-checking Method</a></div><div class="dw_article_header_h2"><a href="#Strategy_Tutorial/Testing_the_Strategy">Testing the Strategy</a></div><div class="dw_article_header_h3"><a href="#Strategy_Tutorial/Testing_with_a_Renko_Feed">Testing with a Renko Feed</a></div><div class="dw_article_header_h3"><a href="#Strategy_Tutorial/Testing_with_a_Custom_Period_Candle_Feed">Testing with a Custom Period Candle Feed</a></div><div class="dw_article_header_h1"><a href="#Strategy_Tutorial/Adding_GUI_to_Strategy">Adding GUI to Strategy</a></div></div><span id="Strategy_Tutorial/Simple_Strategy"><h1>Simple Strategy</h1></span>At first, we will <a href="#Create_a_strategy">create a simple strategy</a> by right-clicking the <i>Strategies</i> node in the <i>JForex</i> platform and choosing <i>New Strategy</i>. <br/><i>JForex</i> platform generates a <a href="https://www.dukascopy.com/wiki/files//StartStrategy.java">new file</a> and opens it in the editor. Try to compile the file by pressing <i>F5</i> or <img src="https://www.dukascopy.com/wiki/files//001_compile_button.png" border="0" /> button. <i>JForex</i> will ask you to name and save the java file on your hard drive. <br/>If you save the file with a custom filename, then don't forget to change the class' name in the generated file. The <u>file name and class name must be the same</u>. E.g. if we save the file as <i>StartStrategy.java</i> then the class name also must be <i>StartStrategy</i><div class="dw_article_geshi_code"><span style="color: #666666; font-style: italic;">//filename - StartStrategy.java</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.*</span><span style="color: #339933;">;</span><br /><br /><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StartStrategy <span style="color: #000000; font-weight: bold;">implements</span> IStrategy <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">//class name must match the file name</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> IEngine engine<span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> IConsole console<span style="color: #339933;">;</span></div><a href="https://www.dukascopy.com/wiki/files//StartStrategy.java">StartStrategy.java</a><br/><span id="Strategy_Tutorial/Modify_onStart"><h2>Modify <i>onStart</i></h2></span>The generated strategy class implements the <a href="#Strategy_API/IStrategy_interface"><i>IStrategy</i></a> interface. The strategt methods are implemented with empty method bodies in the generated strategy java file. Modify the <i><a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IStrategy.html#onStart(com.dukascopy.api.IContext)">onStart</a></i> method. This method is called every time when one starts a strategy. Fill the <i>onStart</i> method's body with the following code:<br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onStart<span style="color: #009900;">&#40;</span>IContext context<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">engine</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getEngine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">console</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getConsole</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">history</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getHistory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">context</span> <span style="color: #339933;">=</span> context<span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">indicators</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getIndicators</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">userInterface</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getUserInterface</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//added code:</span><br />&nbsp; &nbsp; console.<span style="color: #006633;">getOut</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Method onStart() called&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /><span style="color: #009900;">&#125;</span></div>Compile the file by pressing <i>F5</i> or <img src="https://www.dukascopy.com/wiki/files//001_compile_button.png" border="0" /> button. <a href="https://www.dukascopy.com/wiki/files//StartStrategyPrintln.java">StartStrategyPrintln.java</a><span id="Strategy_Tutorial/Run_the_strategy"><h2>Run the strategy</h2></span>Test the strategy by right-clicking the strategy in <i>JForex</i> and choose <i>Local Run</i>. <br/><img src="https://www.dukascopy.com/wiki/files//001_StartStrategyPrintln_starting.png" border="0" /><br/>There are three things that indicate that the strategy has been called:<br/><ol><li>Strategy's icon is changed under the <i>Strategies</i> node - a green triangle is added indicating that the strategy is in running state: <br/><img src="https://www.dukascopy.com/wiki/files//001_StartStrategyPrintln_started_indic_1.png" border="0" /></li><li>In the <i>Messages</i> tab a new message is added: <br/><img src="https://www.dukascopy.com/wiki/files//001_StartStrategyPrintln_started_indic_2.png" border="0" /></li><li>A new tab is opened for started strategy. This tab shows the output of the strategy. In this case we see that the <i>println</i> statement has been executed successfully and printed <i>"Method onStart() called"</i>: <br/><img src="https://www.dukascopy.com/wiki/files//001_StartStrategyPrintln_started_indic_3.png" border="0" /></li></ol><span id="Strategy_Tutorial/Make_Simple_Trade"><h1>Make Simple Trade</h1></span><span id="Strategy_Tutorial/Define_Parameters"><h2>Define Parameters</h2></span>The strategy parameters get set before the execution of the <i>onStart</i> method. Open the strategy's java file in editor and add a parameter to the java file. Read the article <a href="#Strategy_parameters">Strategy parameters</a> to learn more about the strategy parameters. <br/><div class="dw_article_geshi_code">@Configurable<span style="color: #009900;">&#40;</span>value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Instrument value&quot;</span><span style="color: #009900;">&#41;</span><br /><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Instrument</span> myInstrument <span style="color: #339933;">=</span> <span style="color: #003399;">Instrument</span>.<span style="color: #006633;">EURGBP</span><span style="color: #339933;">;</span><br />@Configurable<span style="color: #009900;">&#40;</span>value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Offer Side value&quot;</span>, obligatory<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><br /><span style="color: #000000; font-weight: bold;">public</span> OfferSide myOfferSide<span style="color: #339933;">;</span><br />@Configurable<span style="color: #009900;">&#40;</span>value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Period value&quot;</span><span style="color: #009900;">&#41;</span><br /><span style="color: #000000; font-weight: bold;">public</span> Period myPeriod <span style="color: #339933;">=</span> Period.<span style="color: #006633;">TEN_MINS</span><span style="color: #339933;">;</span></div><a href="https://www.dukascopy.com/wiki/files//StrategyParameters.java">StrategyParameters.java</a><hr><br/>Compile the file. <a href="#Strategy_Tutorial/Run_the_strategy">On strategy launch</a> the <i>"Define Parameters"</i> dialog appears. Here one can modify the strategy parameter values. <br/><img src="https://www.dukascopy.com/wiki/files//001_strateg_param_set_parameters_window.png" border="0" /><br/>After setting the parameter values choose <i>Run</i> to start the strategy. <br/><br/><span id="Strategy_Tutorial/Getting_Bars_and_Ticks"><h2>Getting Bars and Ticks</h2></span>In this part of the tutorial we are using the previously defined parameters. These parameters are used to get <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IBar.html"><i>IBar</i></a> and <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/ITick.html"><i>ITick</i></a> objects. One can visit <a href="#History_bars">History Bars</a> wiki or <a href="#History_ticks">History Ticks</a> wiki to learn more about bars and ticks.  <br/>At first define <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IBar.html"><i>IBar</i></a> and <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/ITick.html"><i>ITick</i></a> object references in the class:<br/><div class="dw_article_geshi_code">&nbsp; <span style="color: #000000; font-weight: bold;">private</span> IBar previousBar<span style="color: #339933;">;</span><br />&nbsp; <span style="color: #000000; font-weight: bold;">private</span> ITick myLastTick<span style="color: #339933;">;</span></div>Initialize <i>previousBar</i> and <i>myLastBar</i> object references in the <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IStrategy.html#onStart(com.dukascopy.api.IContext)"><i>onStart</i></a> method by using <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IHistory.html#getBar(com.dukascopy.api.Instrument,%20com.dukascopy.api.Period,%20com.dukascopy.api.OfferSide,%20int)"><i>IHistory.getBar</i></a> and <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IHistory.html#getLastTick(com.dukascopy.api.Instrument)"><i>IHistory.getLastTick</i></a> methods:<br/><div class="dw_article_geshi_code">&nbsp; previousBar <span style="color: #339933;">=</span> history.<span style="color: #006633;">getBar</span><span style="color: #009900;">&#40;</span>myInstrument, myPeriod, myOfferSide, <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; myLastTick <span style="color: #339933;">=</span> history.<span style="color: #006633;">getLastTick</span><span style="color: #009900;">&#40;</span>myInstrument<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div>Next we should give some output so one can check values in the chart and in the output of the strategy:<br/><div class="dw_article_geshi_code">&nbsp; console.<span style="color: #006633;">getOut</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>previousBar<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; console.<span style="color: #006633;">getOut</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>myLastTick<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div>Before to run the strategy, one needs to <a href="#Subscribe_to_an_instrument">subscribe to an instrument</a> by using a <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IContext.html#setSubscribedInstruments(java.util.Set,%20boolean)"><i>IContext.setSubscribedInstruments</i></a> method. Put the following code <u>before</u> the <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IBar.html"><i>IBar</i></a> initialization code:<br/><div class="dw_article_geshi_code">&nbsp; Set<span style="color: #339933;">&lt;</span>Instrument<span style="color: #339933;">&gt;</span> instruments <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashSet<span style="color: #339933;">&lt;</span>Instrument<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; instruments.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>myInstrument<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; context.<span style="color: #006633;">setSubscribedInstruments</span><span style="color: #009900;">&#40;</span>instruments, <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div>Compile the file. <a href="https://www.dukascopy.com/wiki/files//BarsAndTicks.java">BarsAndTicks.java</a><hr><br/>When one <a href="#Strategy_Tutorial/Simple_Strategy">runs the strategy</a>, three things happen:<br/><ol><li> If the chosen <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/Instrument.html">instrument</a> is not in the list of <i>Instruments</i> window, it is added (that's why we need to <a href="#Subscribe_to_an_instrument">subscribe an instrument</a>; if the instrument is not subscribed, then the strategy will not work)<br/><img src="https://www.dukascopy.com/wiki/files//001_strateg_Instruments_added.png" border="0" /></li><li> The message in <i>Messages</i> tab is displayed. The message also shows the values of the <a href="#Strategy_parameters">parameters</a>. <br/><img src="https://www.dukascopy.com/wiki/files//001_strateg_Instruments_added_Messages.png" border="0" /></li><li> The output messages from strategy are displayed in strategy's tab.<br/><img src="https://www.dukascopy.com/wiki/files//001_strateg_Instruments_added_Strategy_output.png" border="0" /></li></ol>Notice the open and close values of the bar. These will be required to compare chart values and bar values. <br/><hr><br/>Compare the strategy's output with chart output!<br/>Open the desired chart according to <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/Instrument.html">instrument</a> that will be selected in the <i>"Define Parameters"</i> dialog. Set the same <i>Period Value</i> for the chart as it will be in <i>"Define Parameters"</i> dialog. Next, <a href="#Strategy_Tutorial/Simple_Strategy">run the strategy</a>. Compare the <i>Open, Close, High, Low,</i> and <i>Volume</i> parameters from strategy's output tab and chart's last completed bar. These must be the same:<br/> <img src="https://www.dukascopy.com/wiki/files//001_strateg_Instruments_added_Strategy_output.png" border="0" /><br/> <img src="https://www.dukascopy.com/wiki/files//001_strateg_Instruments_added_compare_2.png" border="0" /><br/><span id="Strategy_Tutorial/Make_a_Trade"><h2>Make a Trade</h2></span>At first write an import statement to import <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IEngine.OrderCommand.html"><i>OrderCommand</i></a> enum. This enum defines commands - <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IEngine.OrderCommand.html#SELL"><i>SELL</i></a> and <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IEngine.OrderCommand.html#BUY"><i>BUY</i></a>. We will need these enum constants later. <br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.IEngine.OrderCommand</span><span style="color: #339933;">;</span></div>In this example we are using the previously created strategy java file - <a href="https://www.dukascopy.com/wiki/files//BarsAndTicks.java">BarsAndTicks.java</a>. Now add some code for trading. At first write a line of code that will decide whether to sell or buy. In this case we are making this decision based upon the last completed bar. All of the following code is added to the <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IStrategy.html#onStart(com.dukascopy.api.IContext)"><i>onStart</i></a> method. If the bar's <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IBar.html#getOpen()"><i>getOpen</i></a> method's retrieved value is smaller than the <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IBar.html#getClose()"><i>getClose</i></a> value (green bar), then we will buy, if opposite (red bar), then we will sell:<br/><div class="dw_article_geshi_code">OrderCommand myCommand <span style="color: #339933;">=</span> previousBar.<span style="color: #006633;">getOpen</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> previousBar.<span style="color: #006633;">getClose</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> OrderCommand.<span style="color: #006633;">SELL</span> <span style="color: #339933;">:</span> OrderCommand.<span style="color: #006633;">BUY</span><span style="color: #339933;">;</span></div>When we now what kind of trading operation we will do, we can execute our <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IEngine.OrderCommand.html"><i>OrderCommand</i></a> by using an <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IEngine.html"><i>IEngine</i>'s</a> method <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IEngine.html#submitOrder(java.lang.String,%20com.dukascopy.api.Instrument,%20com.dukascopy.api.IEngine.OrderCommand,%20double)"><i>submitOrder</i></a>. The <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IEngine.html#submitOrder(java.lang.String,%20com.dukascopy.api.Instrument,%20com.dukascopy.api.IEngine.OrderCommand,%20double)"><i>submitOrder</i></a> method takes as it's parameter a <i>String</i> object - the order label. Remember that this label for every order must be <b>unique</b>.<br/><div class="dw_article_geshi_code">engine.<span style="color: #006633;">submitOrder</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MyStrategyOrder2&quot;</span>, myInstrument, myCommand, <span style="color: #cc66cc;">0.1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div> <br/>Here is the complete java file - <a href="https://www.dukascopy.com/wiki/files//BarsAndTicksTrade.java">BarsAndTicksTrade.java</a><hr><br/>Compile the file and <a href="#Strategy_Tutorial/Simple_Strategy">run the strategy</a>. Notice that in the <i>Positions</i> tab there is a new entry where <i>Ext.ID</i> equals with <i>"MyStrategyOrder2"</i>. This is the order label which we gave as the parameter to the <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IEngine.html#submitOrder(java.lang.String,%20com.dukascopy.api.Instrument,%20com.dukascopy.api.IEngine.OrderCommand,%20double)"><i>IEngine.submitOrder</i></a> method. <br/><img src="https://www.dukascopy.com/wiki/files//001_BarsAndTicksTrade_result_1.png" border="0" /><br/>One can close the order by selecting the order's checkbox in <i>Positions</i> tab then right-clicking and choosing <i>Close Position</i>:<br/><img src="https://www.dukascopy.com/wiki/files//001_BarsAndTicksTrade_result_2.png" border="0" /><br/><span id="Strategy_Tutorial/Trade_According_to_Last_Completed_Bar"><h1>Trade According to Last Completed Bar</h1></span>In this part of the tutorial we will modify previously created strategy <a href="https://www.dukascopy.com/wiki/files//BarsAndTicksTrade.java">BarsAndTicksTrade.java</a>. The <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IStrategy.html#onBar(com.dukascopy.api.Instrument,%20com.dukascopy.api.Period,%20com.dukascopy.api.IBar,%20com.dukascopy.api.IBar)"><i>onBar</i></a> method is called on every bar for every basic period and instrument that the application is <a href="#Subscribe_to_an_instrument">subscribed</a> on. To work with specific instruments only in <i>onBar</i> method, we will need to filter them out. In this example we will show how to log the order's events with an <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IStrategy.html#onMessage(com.dukascopy.api.IMessage)"><i>onMessage</i></a> method. To log messages related to the order from all other messages, we will need to filter them out. In this example we're simulating logging by simply printing out to the strategy's output tab.<br/><span id="Strategy_Tutorial/Create_the_Logic_in_the_onBar_method"><h2>Create the Logic in the <i>onBar</i> method</h2></span>Move our previously created strategy's logic from the <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IStrategy.html#onStart(com.dukascopy.api.IContext)"><i>onStart</i></a> method to <i>onBar</i> method. Here is the <i>onStart</i> method after movement:<br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onStart<span style="color: #009900;">&#40;</span>IContext context<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">engine</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getEngine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">console</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getConsole</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">history</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getHistory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">context</span> <span style="color: #339933;">=</span> context<span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">indicators</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getIndicators</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">userInterface</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getUserInterface</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//subscribe an instrument:</span><br />&nbsp; &nbsp; &nbsp; &nbsp; Set<span style="color: #339933;">&lt;</span>Instrument<span style="color: #339933;">&gt;</span> instruments <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashSet<span style="color: #339933;">&lt;</span>Instrument<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; instruments.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>myInstrument<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">setSubscribedInstruments</span><span style="color: #009900;">&#40;</span>instruments, <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div><br/>Define a new instance parameter of <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IOrder.html"><i>IOrder</i></a> type. We will need this <i>IOrder</i> object later on to verify existing orders with the same label:<br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">private</span> IOrder order<span style="color: #339933;">;</span></div>Next, implement the <i>onBar</i> method as follows: <br/><div class="dw_article_geshi_code"><span style="color: #666666; font-style: italic;">/*<br />* Implement our business logic here. Filter specific instrument and period. <br />* Get the order and check whether the order with the same label exists in open state, <br />* if yes then close it. Make a decision and submit a new order. <br />*/</span> &nbsp; &nbsp;<br /><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onBar<span style="color: #009900;">&#40;</span><span style="color: #003399;">Instrument</span> instrument, Period period, IBar askBar, IBar bidBar<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>instrument.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>myInstrument<span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span>period.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>myPeriod<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//quit</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; <br />&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//check and close the order:</span><br />&nbsp; &nbsp; order <span style="color: #339933;">=</span> engine.<span style="color: #006633;">getOrder</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MyStrategyOrder&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>order <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> engine.<span style="color: #006633;">getOrders</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span>order<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; order.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; order.<span style="color: #006633;">waitForUpdate</span><span style="color: #009900;">&#40;</span>IOrder.<span style="color: #006633;">State</span>.<span style="color: #006633;">CLOSED</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//wait till the order is closed</span><br />&nbsp; &nbsp; &nbsp; &nbsp; console.<span style="color: #006633;">getOut</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Order &quot;</span> <span style="color: #339933;">+</span> order.<span style="color: #006633;">getLabel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; is closed&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>order <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; console.<span style="color: #006633;">getOut</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;No order to close&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//if previousBar is GREEN (bullish) then BUY, if RED (bearish) then SELL &nbsp;</span><br />&nbsp; &nbsp; previousBar <span style="color: #339933;">=</span> myOfferSide <span style="color: #339933;">==</span> OfferSide.<span style="color: #006633;">ASK</span> <span style="color: #339933;">?</span> askBar <span style="color: #339933;">:</span> bidBar<span style="color: #339933;">;</span><br />&nbsp; &nbsp; console.<span style="color: #006633;">getOut</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; || PreviousBar- --> &quot;</span> <span style="color: #339933;">+</span> previousBar <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; || Period- --> &quot;</span> <span style="color: #339933;">+</span> period <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; || Instrument- --> &quot;</span> <span style="color: #339933;">+</span> instrument<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; OrderCommand myCommand <span style="color: #339933;">=</span> previousBar.<span style="color: #006633;">getOpen</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> previousBar.<span style="color: #006633;">getClose</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> OrderCommand.<span style="color: #006633;">SELL</span> <span style="color: #339933;">:</span> OrderCommand.<span style="color: #006633;">BUY</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; engine.<span style="color: #006633;">submitOrder</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MyStrategyOrder&quot;</span>, myInstrument, myCommand, <span style="color: #cc66cc;">0.1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br /><span style="color: #009900;">&#125;</span></div><span id="Strategy_Tutorial/Log_an_Order"><h2>Log an Order</h2></span>Implement the <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IStrategy.html#onMessage(com.dukascopy.api.IMessage)"><i>onMessages</i></a> method as the following:<br/><div class="dw_article_geshi_code"><span style="color: #666666; font-style: italic;">/*<br />&nbsp;* filter out from all IMessages the messages related with orders and<br />&nbsp;* log them to the strategy's output tab<br />&nbsp;*/</span><br /><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onMessage<span style="color: #009900;">&#40;</span>IMessage message<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span> &nbsp; &nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>message.<span style="color: #006633;">getOrder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <br />&nbsp; &nbsp; &nbsp; &nbsp; console.<span style="color: #006633;">getOut</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;order: &quot;</span> <span style="color: #339933;">+</span> message.<span style="color: #006633;">getOrder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getLabel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; || message content: &quot;</span> <span style="color: #339933;">+</span> message.<span style="color: #006633;">getContent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /><span style="color: #009900;">&#125;</span></div><a href="#IConsole/Logging_values">Click here</a> to learn more about logging. <br/>Here is the complete strategy's java file - <a href="https://www.dukascopy.com/wiki/files//OnBarExample.java">OnBarExample.java</a><hr><br/>One can check how the strategy works in real situations with a real historic data by using a <a href="#Historical_Tester"><i>Historical Tester</i></a>. We test our strategy based upon the data of the last day. To do this, select the <i>Tools-Historical Tester</i> to open the historical tester's tab. Choose the strategy from a drop-down list; press the <i>Instrument</i> button to set the desired instrument; set the period in a drop-down list to <i>Last day</i>; set the period to <i>Ticks</i>. Finally, press the <img src="https://www.dukascopy.com/wiki/files//001_Historical_Tester_start_button.png" border="0" /> button to start the test. <br/>If You're interested in more details how to work with <i>Historical Tester</i>, visit the <a href="#Historical_Tester"><i>Historical Tester</i>'s wiki</a>. <br/><img src="https://www.dukascopy.com/wiki/files//001_Historical_Tester_panel.png" border="0" /><br/>Test results opens in a browser by default.<br/><br/><span id="Strategy_Tutorial/Trade_According_to_SMA_Trend"><h1>Trade According to SMA Trend</h1></span>The strategy of this part of the tutorial will trade according to changes of <i>SMA</i> indicator - buy on up-trend and sell on down-trend. We will use a SMA (Simple Moving Average) indicator and upgrade our previously <a href="https://www.dukascopy.com/wiki/files//OnBarExample.java">created strategy java file</a>. <br/><br/>The idea is to use a <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IIndicators.html#sma(com.dukascopy.api.Instrument,%20com.dukascopy.api.Period,%20com.dukascopy.api.OfferSide,%20com.dukascopy.api.IIndicators.AppliedPrice,%20int,%20com.dukascopy.api.Filter,%20int,%20long,%20int)"><i>Indicators.sma</i></a> method to get values for last two completed bars (second-to-last and last) and make a decision according to values of these bars. In this case we use the <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IIndicators.html#sma(com.dukascopy.api.Instrument,%20com.dukascopy.api.Period,%20com.dukascopy.api.OfferSide,%20com.dukascopy.api.IIndicators.AppliedPrice,%20int,%20com.dukascopy.api.Filter,%20int,%20long,%20int)"><i>sma</i></a> method which takes candle intervals as parameters. Click <a href="#Indicator_Calculation/Calculate_indicator_by_candle_interval">here</a> to learn more how to use candle intervals for indicator calculation.<br/><br/>Let's prepare the strategy for our new implementation of the <i>onBar</i> method. We will need to set the time period attribute for the <i>sma</i> method and few constants for the <i>sma</i> method's returned array. Plus we will add one utility method to print messages to the console:<br/><div class="dw_article_geshi_code">@Configurable<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SMA time period&quot;</span><span style="color: #009900;">&#41;</span><br /><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> smaTimePeriod <span style="color: #339933;">=</span> <span style="color: #cc66cc;">30</span><span style="color: #339933;">;</span><br /><br /><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> PREV <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> SECOND_TO_LAST <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br /><br /><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> printMe<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> toPrint<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; console.<span style="color: #006633;">getOut</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>toPrint<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /><span style="color: #009900;">&#125;</span></div><br/>Here is a complete code of the <i>onBar</i> method:<br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onBar<span style="color: #009900;">&#40;</span><span style="color: #003399;">Instrument</span> instrument, Period period, IBar askBar, IBar bidBar<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span> &nbsp; &nbsp; <br /><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>instrument.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>myInstrument<span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span>period.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>myPeriod<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//quit</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br /><br />&nbsp; &nbsp; IEngine.<span style="color: #006633;">OrderCommand</span> myCommand <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> candlesBefore <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span>, candlesAfter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <br />&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//get SMA values of 2nd-to last and last (two last completed) bars</span><br />&nbsp; &nbsp; previousBar <span style="color: #339933;">=</span> myOfferSide <span style="color: #339933;">==</span> OfferSide.<span style="color: #006633;">ASK</span> <span style="color: #339933;">?</span> askBar <span style="color: #339933;">:</span> bidBar<span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">long</span> currBarTime <span style="color: #339933;">=</span> previousBar.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> sma<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> indicators.<span style="color: #006633;">sma</span><span style="color: #009900;">&#40;</span>instrument, period, myOfferSide, IIndicators.<span style="color: #006633;">AppliedPrice</span>.<span style="color: #006633;">CLOSE</span>, <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; smaTimePeriod, Filter.<span style="color: #006633;">NO_FILTER</span>, candlesBefore, currBarTime, candlesAfter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /><br />&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">/*print some message so we can later compare the results with a chart,<br />&nbsp; &nbsp; &nbsp;*If SMA is up-trend (green line in sample picture) execute BUY order. <br />&nbsp; &nbsp; &nbsp;*If SMA is down-trend (red line in sample picture) execute SELL order.<br />&nbsp; &nbsp; &nbsp;*/</span><br />&nbsp; &nbsp; printMe<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bar SMA Values: Second-to-last = %.5f; Last Completed = %.5f&quot;</span>, sma<span style="color: #009900;">&#91;</span>SECOND_TO_LAST<span style="color: #009900;">&#93;</span>, sma<span style="color: #009900;">&#91;</span>PREV<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>sma<span style="color: #009900;">&#91;</span>PREV<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> sma<span style="color: #009900;">&#91;</span>SECOND_TO_LAST<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; printMe<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SMA in up-trend&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//indicator goes up</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;myCommand <span style="color: #339933;">=</span> IEngine.<span style="color: #006633;">OrderCommand</span>.<span style="color: #006633;">BUY</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>sma<span style="color: #009900;">&#91;</span>PREV<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;</span> sma<span style="color: #009900;">&#91;</span>SECOND_TO_LAST<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; printMe<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SMA in down-trend&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//indicator goes down</span><br />&nbsp; &nbsp; &nbsp; &nbsp; myCommand <span style="color: #339933;">=</span> IEngine.<span style="color: #006633;">OrderCommand</span>.<span style="color: #006633;">SELL</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /><br />&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">/*check if the order already exists. If exists, then check if the processing order's command is the same as myCommand. <br />&nbsp; &nbsp; &nbsp;* If it is the same, then do nothing (let the order stay in open state and continues to processing).<br />&nbsp; &nbsp; &nbsp;* If the order command is different (SMA trend changes direction) from the current order's command,<br />&nbsp; &nbsp; &nbsp;* then close the opened order and create a new one:<br />&nbsp; &nbsp; &nbsp;*/</span><br />&nbsp; &nbsp; order <span style="color: #339933;">=</span> engine.<span style="color: #006633;">getOrder</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MyStrategyOrder&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>order <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> engine.<span style="color: #006633;">getOrders</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span>order<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> order.<span style="color: #006633;">getOrderCommand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> myCommand<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; order.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; order.<span style="color: #006633;">waitForUpdate</span><span style="color: #009900;">&#40;</span>IOrder.<span style="color: #006633;">State</span>.<span style="color: #006633;">CLOSED</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//wait till the order is closed</span><br />&nbsp; &nbsp; &nbsp; &nbsp; console.<span style="color: #006633;">getOut</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Order &quot;</span> <span style="color: #339933;">+</span> order.<span style="color: #006633;">getLabel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; is closed&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <br />&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//if the order is new or there is no order with the same label, then create a new order:</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>order <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span>engine.<span style="color: #006633;">getOrders</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span>order<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; engine.<span style="color: #006633;">submitOrder</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MyStrategyOrder&quot;</span>, instrument, myCommand, <span style="color: #cc66cc;">0.1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /><br /><span style="color: #009900;">&#125;</span></div>Here is a complete strategy's java file - <a href="https://www.dukascopy.com/wiki/files//SMASampleTrade.java">SMASampleTrade.java</a><hr><br/>Test our strategy for few days with <i>Historical Tester</i>. When the chart opens, confirm that the period is the same as you gave as a parameter. We're using following parameters in this sample:<br/><img src="https://www.dukascopy.com/wiki/files//001_SMASampleTrade_parameters.png" border="0" /><br/>Add <i>SMA</i> indicator to the chart by clicking on the <i>f(x)</i> button, then choose <i>Add Indicator</i>. In a <i>Quick Filter</i> search for <i>SMA - Simple Moving Average</i> and set the following parameters:<br/><img src="https://www.dukascopy.com/wiki/files//001_SMASampleTrade_indicator_parameters.png" border="0" /><br/> <br/>One can see, that the decision is made upon the two last completed bars. If a <i>SMA</i> trend line starts to go up then we sell, if down then buy. Check also the strategy's output - here you can see the <i>SMA</i> indicators for two last completed bars (last and second-to-last) and the order's messages.<br/><img src="https://www.dukascopy.com/wiki/files//001_SMASampleTrade_history_chart.png" border="0" /> <img src="https://www.dukascopy.com/wiki/files//001_SMASampleTrade_output_results.png" border="0" /><br/>Click <a href="#Indicator_Calculation">here</a> to learn more about the <i>Indicator Calculation</i>. <br/><br/><span id="Strategy_Tutorial/Representing_Events_on_Chart"><h1>Representing Events on Chart</h1></span>In this part of the tutorial one can learn how to <a href="#Create_and_manage_chart_objects">add chart objects to a chart</a> and how to customize them. We will add and customize an <a href="#SMA_Simple"><i>SMA</i> indicator</a>, <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/drawings/IOhlcChartObject.html"><i>IOhlcChartObject</i></a>, <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/drawings/ISignalDownChartObject.html">ISignalDownChartObject</a>, and <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/drawings/ISignalUpChartObject.html"><i>ISignalUpChartObject</i></a> objects. This strategy will make orders according to the <i>SMA</i> values and add the <i>ISignalDownChartObject</i> or the <i>ISignalUpChartObject</i> object to the chart in the point of time-line when a new order is created. We are building this new strategy upon the previously created <a href="https://www.dukascopy.com/wiki/files//SMASampleTrade.java">SMASampleTrade.java</a> file. Click <a href="#Chart_object_catalog">here</a> to learn more about adding objects to a chart. <br/><br/>At first, add the following imports:<br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.drawings.IChartObjectFactory</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.drawings.IOhlcChartObject</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.indicators.OutputParameterInfo.DrawingStyle</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.Color</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.Dimension</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.Font</span><span style="color: #339933;">;</span></div>We will use <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IChart.html"><i>IChart</i></a> and <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/drawings/IChartObjectFactory.html"><i>IChartObjectFactory</i></a> objects to work with charts. So we create an instance parameter for each of them. As addition, we are creating one instance parameter of integer type - we will need it later:<br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">private</span> IChart openedChart<span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">private</span> IChartObjectFactory factory<span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> signals<span style="color: #339933;">;</span></div>When running the strategy, one will be able to choose whether to add or not the indicator and <i>OHLC</i> values to the chart, and whether or not to close the chart after the strategy is closed. So, add following parameters to the code:<br/><div class="dw_article_geshi_code">@Configurable<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Add OHLC Index to chart&quot;</span><span style="color: #009900;">&#41;</span><br /><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> addOHLC <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />@Configurable<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Filter&quot;</span><span style="color: #009900;">&#41;</span><br /><span style="color: #000000; font-weight: bold;">public</span> Filter filter <span style="color: #339933;">=</span> Filter.<span style="color: #006633;">WEEKENDS</span><span style="color: #339933;">;</span><br />@Configurable<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Draw SMA&quot;</span><span style="color: #009900;">&#41;</span><br /><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> drawSMA <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />@Configurable<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Close chart on onStop&quot;</span><span style="color: #009900;">&#41;</span><br /><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> closeChart<span style="color: #339933;">;</span></div>Next, initialize the instance variables needed to add an <i>SMA</i> indicator to the chart and call a method (<i>addToChart</i>) which will do the job. The <i>onStart</i> method:<br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onStart<span style="color: #009900;">&#40;</span>IContext context<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">engine</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getEngine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">console</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getConsole</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">history</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getHistory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">context</span> <span style="color: #339933;">=</span> context<span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">indicators</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getIndicators</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">userInterface</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getUserInterface</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//4.initialize the previously defined instance variables:</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">openedChart</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getChart</span><span style="color: #009900;">&#40;</span>myInstrument<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">factory</span> <span style="color: #339933;">=</span> openedChart.<span style="color: #006633;">getChartObjectFactory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /><br />&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//subscribe an instrument:</span><br />&nbsp; &nbsp; Set<span style="color: #339933;">&lt;</span>Instrument<span style="color: #339933;">&gt;</span> instruments <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashSet<span style="color: #339933;">&lt;</span>Instrument<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; instruments.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>myInstrument<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; context.<span style="color: #006633;">setSubscribedInstruments</span><span style="color: #009900;">&#40;</span>instruments, <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp;<br /><br />&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//5.Check whether the user wants to add SMA indicator to the chart.</span><br />&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//If yes, then executea addToChart method. addToChart method returns boolean value</span><br />&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//if it can add an indicator to the chart. If it cannot add, then an error message is printed to the output</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>drawSMA<span style="color: #009900;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>addToChart<span style="color: #009900;">&#40;</span>openedChart<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printMeError<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Indicators did not get plotted on chart. Check the chart values!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /><span style="color: #009900;">&#125;</span></div>Modify the <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IStrategy.html#onStop()"><i>onStop</i></a> method so it takes into consideration the users choice whether to close or not the chart if the strategy is going to close:<br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onStop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>closeChart<span style="color: #009900;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">closeChart</span><span style="color: #009900;">&#40;</span>openedChart<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /><span style="color: #009900;">&#125;</span></div>As an addition to the previously created logic, we will add to the chart an <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/drawings/ISignalDownChartObject.html"><i>ISignalDownChartObject</i></a> or <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/drawings/ISignalUpChartObject.html"><i>ISignalUpChartObject</i></a> object every time the <i>onBar</i> method is invoked for our instrument and period. Consider the following code in the <i>onBar</i> method:<br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onBar<span style="color: #009900;">&#40;</span><span style="color: #003399;">Instrument</span> instrument, Period period, IBar askBar, IBar bidBar<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span> &nbsp; &nbsp; <br /><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>instrument.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>myInstrument<span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span>period.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>myPeriod<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//quit</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br /><br />&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> candlesBefore <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span>, candlesAfter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <br />&nbsp; &nbsp; previousBar <span style="color: #339933;">=</span> myOfferSide <span style="color: #339933;">==</span> OfferSide.<span style="color: #006633;">ASK</span> <span style="color: #339933;">?</span> askBar <span style="color: #339933;">:</span> bidBar<span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">long</span> currBarTime <span style="color: #339933;">=</span> previousBar.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> sma<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> indicators.<span style="color: #006633;">sma</span><span style="color: #009900;">&#40;</span>instrument, period, myOfferSide, IIndicators.<span style="color: #006633;">AppliedPrice</span>.<span style="color: #006633;">CLOSE</span>, <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; smaTimePeriod, Filter.<span style="color: #006633;">NO_FILTER</span>, candlesBefore, currBarTime, candlesAfter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp;<br />&nbsp; &nbsp; printMe<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bar SMA Values: Second-to-last = %.5f; Last Completed = %.5f&quot;</span>, sma<span style="color: #009900;">&#91;</span>SECOND_TO_LAST<span style="color: #009900;">&#93;</span>, sma<span style="color: #009900;">&#91;</span>PREV<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <br />&nbsp; &nbsp; IEngine.<span style="color: #006633;">OrderCommand</span> myCommand <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; printMe<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bar SMA Values: Second-to-last = %.5f; Last Completed = %.5f&quot;</span>, sma<span style="color: #009900;">&#91;</span>SECOND_TO_LAST<span style="color: #009900;">&#93;</span>, sma<span style="color: #009900;">&#91;</span>PREV<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>sma<span style="color: #009900;">&#91;</span>PREV<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> sma<span style="color: #009900;">&#91;</span>SECOND_TO_LAST<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; printMe<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SMA in up-trend&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//indicator goes up</span><br />&nbsp; &nbsp; &nbsp; &nbsp; myCommand <span style="color: #339933;">=</span> IEngine.<span style="color: #006633;">OrderCommand</span>.<span style="color: #006633;">BUY</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>sma<span style="color: #009900;">&#91;</span>PREV<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;</span> sma<span style="color: #009900;">&#91;</span>SECOND_TO_LAST<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; printMe<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SMA in down-trend&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//indicator goes down</span><br />&nbsp; &nbsp; &nbsp; &nbsp; myCommand <span style="color: #339933;">=</span> IEngine.<span style="color: #006633;">OrderCommand</span>.<span style="color: #006633;">SELL</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; order <span style="color: #339933;">=</span> engine.<span style="color: #006633;">getOrder</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MyStrategyOrder&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>order <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> engine.<span style="color: #006633;">getOrders</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span>order<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> order.<span style="color: #006633;">getOrderCommand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> myCommand<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; order.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; order.<span style="color: #006633;">waitForUpdate</span><span style="color: #009900;">&#40;</span>IOrder.<span style="color: #006633;">State</span>.<span style="color: #006633;">CLOSED</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//wait till the order is closed</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.<span style="color: #006633;">getOut</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Order &quot;</span> <span style="color: #339933;">+</span> order.<span style="color: #006633;">getLabel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; is closed&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>order <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span>engine.<span style="color: #006633;">getOrders</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span>order<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; engine.<span style="color: #006633;">submitOrder</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MyStrategyOrder&quot;</span>, instrument, myCommand, <span style="color: #cc66cc;">0.1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//7.we will draw an arrow when makeing an order. </span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//So we need a time of the current bar - to get it, we add one period of a bar to a time of the last completed bar (previousBar). </span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">long</span> time <span style="color: #339933;">=</span> previousBar.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> myPeriod.<span style="color: #006633;">getInterval</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//8.creating a IChartObject - an up-array or down-array and add a text label to the array. </span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//finally, add a IChartObject(array in this case) to the chart. </span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IChartObject signal <span style="color: #339933;">=</span> myCommand.<span style="color: #006633;">isLong</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; factory.<span style="color: #006633;">createSignalUp</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;signalUpKey&quot;</span> <span style="color: #339933;">+</span> signals<span style="color: #339933;">++</span>, time, previousBar.<span style="color: #006633;">getLow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> instrument.<span style="color: #006633;">getPipValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">:</span> factory.<span style="color: #006633;">createSignalDown</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;signalDownKey&quot;</span> <span style="color: #339933;">+</span> signals<span style="color: #339933;">++</span>, time, previousBar.<span style="color: #006633;">getHigh</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> instrument.<span style="color: #006633;">getPipValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; signal.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;delta SMA %+.7f&quot;</span>, sma<span style="color: #009900;">&#91;</span>PREV<span style="color: #009900;">&#93;</span> <span style="color: #339933;">-</span> sma<span style="color: #009900;">&#91;</span>SECOND_TO_LAST<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Font</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Monospaced&quot;</span>, <span style="color: #003399;">Font</span>.<span style="color: #006633;">BOLD</span>, <span style="color: #cc66cc;">12</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; openedChart.<span style="color: #006633;">addToMainChart</span><span style="color: #009900;">&#40;</span>signal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br /><br /><span style="color: #009900;">&#125;</span><span style="color: #666666; font-style: italic;">//end of onBar method</span></div>Add a new utility method for printing error messages to the strategy's output:<br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> printMeError<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> o<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; console.<span style="color: #006633;">getErr</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>o<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /><span style="color: #009900;">&#125;</span></div>At last, implement the <i>addToChart</i> method. This method will add the <i>SMA</i> indicator and optionally the <i>OHLC</i> variables to the chart:<br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> addToChart<span style="color: #009900;">&#40;</span>IChart chart<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>chart <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; printMeError<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;chart for &quot;</span> <span style="color: #339933;">+</span> myInstrument <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; not opened!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>chart.<span style="color: #006633;">getSelectedOfferSide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">myOfferSide</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; printMeError<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;chart offer side is not &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">myOfferSide</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>chart.<span style="color: #006633;">getSelectedPeriod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">myPeriod</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; printMeError<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;chart period is not &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">myPeriod</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>chart.<span style="color: #006633;">getFilter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">filter</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; printMeError<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;chart filter is not &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">filter</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; chart.<span style="color: #006633;">addIndicator</span><span style="color: #009900;">&#40;</span>indicators.<span style="color: #006633;">getIndicator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SMA&quot;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>smaTimePeriod<span style="color: #009900;">&#125;</span>, <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Color</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #003399;">Color</span>.<span style="color: #006633;">GREEN</span><span style="color: #009900;">&#125;</span>, <span style="color: #000000; font-weight: bold;">new</span> DrawingStyle<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>DrawingStyle.<span style="color: #006633;">LINE</span><span style="color: #009900;">&#125;</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <br />&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//if user has choosed to show the OHLC values, then add them to the chart:</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>addOHLC<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//If finds an existing ohlc object, then assign this object to the ohlc ref. variable</span><br />&nbsp; &nbsp; &nbsp; &nbsp; IOhlcChartObject ohlc <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>IChartObject obj <span style="color: #339933;">:</span> chart.<span style="color: #006633;">getAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>obj <span style="color: #000000; font-weight: bold;">instanceof</span> IOhlcChartObject<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ohlc <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>IOhlcChartObject<span style="color: #009900;">&#41;</span> obj<span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//if cannot find existing ohlc object, then create new one and assign to ohlc ref. variable</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ohlc <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ohlc <span style="color: #339933;">=</span> chart.<span style="color: #006633;">getChartObjectFactory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">createOhlcInformer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ohlc.<span style="color: #006633;">setPreferredSize</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Dimension</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">100</span>, <span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chart.<span style="color: #006633;">addToMainChart</span><span style="color: #009900;">&#40;</span>ohlc<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//show the ohlc index</span><br />&nbsp; &nbsp; &nbsp; &nbsp; ohlc.<span style="color: #006633;">setShowIndicatorInfo</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span><br /><span style="color: #009900;">&#125;</span><span style="color: #666666; font-style: italic;">//end of addToChart method</span></div>Here is the complete strategy's java file - <a href="https://www.dukascopy.com/wiki/files//ChartUsage.java">ChartUsage.java</a>. <br/><hr><br/>Test the strategy with <a href="#Historical_Tester"> <i>Historical Tester</i></a>. Don't choose to close the chart on <i>onStop</i> method when testing with <i>Historical Tester</i> (it will show error messages), one should manually close the old chart before running a new test. In this example we are running our strategy with the following parameters:<br/><img src="https://www.dukascopy.com/wiki/files//001_ChartUsage_test_parameters.png" border="0" /><br/>Here is a resulting chart:<br/><img src="https://www.dukascopy.com/wiki/files//001_ChartUsage_output_chart.png" border="0" /><br/><br/><span id="Strategy_Tutorial/Add_Stop_Loss_and_Take_Profit"><h1>Add Stop Loss and Take Profit</h1></span>In this part of the tutorial we <a href="#Set_Stop_Loss_price">use stop-loss</a> (<i>SL</i>) and take profit (<i>TP</i>) values of an order. We will modify the previously created strategy java file - <a href="https://www.dukascopy.com/wiki/files//ChartUsage.java">ChartUsage.java</a>. <br/><br/>We begin by adding new imports and removing unnecessarily ones. Previously we assigned signal-up and signal-down objects to a <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IChartObject.html"><i>IChartObject</i></a> type, but now we will use another (more specific) reference type - <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/drawings/IChartDependentChartObject.html"><i>IChartDependentChartObject</i></a>. By using this kind of object one can indicate that the object will not stick to a bar when added to the chart (with <i>IChartObject</i> object type it is not possible).<br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.drawings.IChartDependentChartObject</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.drawings.ITriangleChartObject</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.drawings.ITextChartObject</span><span style="color: #339933;">;</span><br /><span style="color: #666666; font-style: italic;">//2. Remove unneccesary imports</span><br /><span style="color: #666666; font-style: italic;">//import java.awt.Font;</span></div>Define new instance variables. We will need them later on:<br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> uniqueOrderCounter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">private</span> SMATrend previousSMADirection <span style="color: #339933;">=</span> SMATrend.<span style="color: #006633;">NOT_SET</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">private</span> SMATrend currentSMADirection <span style="color: #339933;">=</span> SMATrend.<span style="color: #006633;">NOT_SET</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">private</span> Map<span style="color: #339933;">&lt;</span>IOrder, Boolean<span style="color: #339933;">&gt;</span> createdOrderMap <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span>IOrder, Boolean<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> shorLineCounter<span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> textCounterOldSL<span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> textCounterNewSL<span style="color: #339933;">;</span></div>As mentioned before, we are going to use <i>SL</i> and <i>TP</i> values of an order, so we add new parameters to be able to set the necessary values at the strategy's startup. The <i>breakEventPips</i> variable is used to set the level of profit-pips. When this level of profit in pips is reached, the order's <i>SL</i> value is set to the order's open-price level.<br/><div class="dw_article_geshi_code">@Configurable<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Stop loss in pips&quot;</span><span style="color: #009900;">&#41;</span><br /><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> stopLossPips <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span><br />@Configurable<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Take profit in pips&quot;</span><span style="color: #009900;">&#41;</span><br /><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> takeProfitPips <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span><br />@Configurable<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Break event pips&quot;</span><span style="color: #009900;">&#41;</span><br /><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">double</span> breakEventPips <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span></div>Define an enum which will hold constants of all possible <i>SMA</i> trend line states:<br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">enum</span> SMATrend <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; UP, DOWN, NOT_SET<span style="color: #339933;">;</span><br /><span style="color: #009900;">&#125;</span></div><span id="Strategy_Tutorial/Break_Even_in_onTick"><h2>Break Even in <i>onTick</i></h2></span>Consider the <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IStrategy.html#onTick(com.dukascopy.api.Instrument,%20com.dukascopy.api.ITick)"><i>onTick</i></a> method which gets called on every instrument's tick. We filter out only the instruments in which we are interested in, so the method is executed only when we need to. Later in the <i>onBar</i> method we add every new order to a <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Map.html"><i>Map</i></a> object. Here we go through the <i>Map</i> of orders to see if we have already moved the <i>SL</i> according to the break-even level. If it is not changed, then we check if the profit in pips is greater than <i>breakEventPips</i> parameter. If it is, then we can change the <i>SL</i> value to the order's open price level. Every time the <i>SL</i> value is set to the open price, we add a triangle (by invoking a <i>addBreakToChart</i> method which is described later) to indicate the process visually on the chart. Finally we change the order's <i>SL</i> price and update the entry in the Map. Here is the implementation of <i>onTick</i> method:<br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onTick<span style="color: #009900;">&#40;</span><span style="color: #003399;">Instrument</span> instrument, ITick tick<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>instrument <span style="color: #339933;">!=</span> myInstrument<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Map</span>.<span style="color: #006633;">Entry</span><span style="color: #339933;">&lt;</span>IOrder, Boolean<span style="color: #339933;">&gt;</span> entry <span style="color: #339933;">:</span> createdOrderMap.<span style="color: #006633;">entrySet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; IOrder currentOrder <span style="color: #339933;">=</span> entry.<span style="color: #006633;">getKey</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">boolean</span> currentValue <span style="color: #339933;">=</span> entry.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>currentValue <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">false</span> <span style="color: #339933;">&amp;&amp;</span> currentOrder.<span style="color: #006633;">getProfitLossInPips</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> breakEventPips<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printMe<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Order has profit of &quot;</span> <span style="color: #339933;">+</span> currentOrder.<span style="color: #006633;">getProfitLossInPips</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; pips! Moving the stop loss to the open price.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addBreakToChart<span style="color: #009900;">&#40;</span>currentOrder, tick, currentOrder.<span style="color: #006633;">getStopLossPrice</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, currentOrder.<span style="color: #006633;">getOpenPrice</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//add a line to the chart indicating the SL changes</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentOrder.<span style="color: #006633;">setStopLossPrice</span><span style="color: #009900;">&#40;</span>currentOrder.<span style="color: #006633;">getOpenPrice</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; entry.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /><span style="color: #009900;">&#125;</span><span style="color: #666666; font-style: italic;">//end of onTick method</span></div><span id="Strategy_Tutorial/Trade_According_to_SMA"><h2>Trade According to <i>SMA</i></h2></span>We modify our previously created <i>onBar</i> method. We are changing the <i>onBar</i> method in the way that it will use the <i>SMATrend</i> enum values to check when to create a new order. We are setting the <i>SL</i> and <i>TP</i> values, too. One difference from previous example is that we're not closing the previous order if a new one gets opened. The orders are closed automatically when the <i>SL</i> or <i>TP</i> values are reached. Plus, all new orders are saved in the <i>Map</i> for later checking in the <i>onTick</i> method.<br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onBar<span style="color: #009900;">&#40;</span><span style="color: #003399;">Instrument</span> instrument, Period period, IBar askBar, IBar bidBar<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>instrument.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>myInstrument<span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span>period.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>myPeriod<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//quit</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /><br />&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> candlesBefore <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span>, candlesAfter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">long</span> completedBarTimeL <span style="color: #339933;">=</span> myOfferSide <span style="color: #339933;">==</span> OfferSide.<span style="color: #006633;">ASK</span> <span style="color: #339933;">?</span> askBar.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> bidBar.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> sma<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> indicators.<span style="color: #006633;">sma</span><span style="color: #009900;">&#40;</span>instrument, period, myOfferSide, IIndicators.<span style="color: #006633;">AppliedPrice</span>.<span style="color: #006633;">CLOSE</span>,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; smaTimePeriod, Filter.<span style="color: #006633;">NO_FILTER</span>, candlesBefore, completedBarTimeL, candlesAfter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; printMe<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bar SMA Values: Second-to-last = %.5f; Last Completed = %.5f&quot;</span>, sma<span style="color: #009900;">&#91;</span>SECOND_TO_LAST<span style="color: #009900;">&#93;</span>, sma<span style="color: #009900;">&#91;</span>PREV<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /><br />&nbsp; &nbsp; IEngine.<span style="color: #006633;">OrderCommand</span> myCommand <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; printMe<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bar SMA Values: Second-to-last = %.5f; Last Completed = %.5f&quot;</span>, sma<span style="color: #009900;">&#91;</span>SECOND_TO_LAST<span style="color: #009900;">&#93;</span>, sma<span style="color: #009900;">&#91;</span>PREV<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>sma<span style="color: #009900;">&#91;</span>PREV<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> sma<span style="color: #009900;">&#91;</span>SECOND_TO_LAST<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; printMe<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SMA in up-trend&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//indicator goes up</span><br />&nbsp; &nbsp; &nbsp; &nbsp; myCommand <span style="color: #339933;">=</span> IEngine.<span style="color: #006633;">OrderCommand</span>.<span style="color: #006633;">BUY</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; currentSMADirection <span style="color: #339933;">=</span> SMATrend.<span style="color: #006633;">UP</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>sma<span style="color: #009900;">&#91;</span>PREV<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;</span> sma<span style="color: #009900;">&#91;</span>SECOND_TO_LAST<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; printMe<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SMA in down-trend&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//indicator goes down</span><br />&nbsp; &nbsp; &nbsp; &nbsp; myCommand <span style="color: #339933;">=</span> IEngine.<span style="color: #006633;">OrderCommand</span>.<span style="color: #006633;">SELL</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; currentSMADirection <span style="color: #339933;">=</span> SMATrend.<span style="color: #006633;">DOWN</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /><br />&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> lastTickBid <span style="color: #339933;">=</span> history.<span style="color: #006633;">getLastTick</span><span style="color: #009900;">&#40;</span>myInstrument<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getBid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> lastTickAsk <span style="color: #339933;">=</span> history.<span style="color: #006633;">getLastTick</span><span style="color: #009900;">&#40;</span>myInstrument<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getAsk</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> stopLossValueForLong <span style="color: #339933;">=</span> myInstrument.<span style="color: #006633;">getPipValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> stopLossPips<span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> stopLossValueForShort <span style="color: #339933;">=</span> myInstrument.<span style="color: #006633;">getPipValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> takeProfitPips<span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> stopLossPrice <span style="color: #339933;">=</span> myCommand.<span style="color: #006633;">isLong</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #009900;">&#40;</span>lastTickBid <span style="color: #339933;">-</span> stopLossValueForLong<span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>lastTickAsk <span style="color: #339933;">+</span> stopLossValueForLong<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> takeProfitPrice <span style="color: #339933;">=</span> myCommand.<span style="color: #006633;">isLong</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #009900;">&#40;</span>lastTickBid <span style="color: #339933;">+</span> stopLossValueForShort<span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>lastTickAsk <span style="color: #339933;">-</span> stopLossValueForShort<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /><br />&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//if SMA trend direction is changed, then create a new order</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>currentSMADirection <span style="color: #339933;">!=</span> previousSMADirection<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; previousSMADirection <span style="color: #339933;">=</span> currentSMADirection<span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; IOrder newOrder <span style="color: #339933;">=</span> engine.<span style="color: #006633;">submitOrder</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MyStrategyOrder&quot;</span> <span style="color: #339933;">+</span> uniqueOrderCounter<span style="color: #339933;">++</span>, instrument, myCommand, <span style="color: #cc66cc;">0.1</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1</span>, stopLossPrice, takeProfitPrice<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; createdOrderMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>newOrder, <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>openedChart <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">long</span> time <span style="color: #339933;">=</span> bidBar.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> myPeriod.<span style="color: #006633;">getInterval</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//draw the &nbsp;ISignalDownChartObject in the current bar</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> space <span style="color: #339933;">=</span> myInstrument.<span style="color: #006633;">getPipValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//space up or down from bar for ISignalDownChartObject</span><br />&nbsp; &nbsp; &nbsp; &nbsp; IChartDependentChartObject signal <span style="color: #339933;">=</span> myCommand.<span style="color: #006633;">isLong</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">?</span> factory.<span style="color: #006633;">createSignalUp</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;signalUpKey&quot;</span> <span style="color: #339933;">+</span> signals<span style="color: #339933;">++</span>, time, bidBar.<span style="color: #006633;">getLow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> space<span style="color: #009900;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">:</span> factory.<span style="color: #006633;">createSignalDown</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;signalDownKey&quot;</span> <span style="color: #339933;">+</span> signals<span style="color: #339933;">++</span>, time, bidBar.<span style="color: #006633;">getHigh</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> space<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; signal.<span style="color: #006633;">setStickToCandleTimeEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; signal.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MyStrategyOrder&quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>uniqueOrderCounter <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; openedChart.<span style="color: #006633;">addToMainChart</span><span style="color: #009900;">&#40;</span>signal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /><br /><span style="color: #009900;">&#125;</span><span style="color: #666666; font-style: italic;">//end of onBar method</span></div><span id="Strategy_Tutorial/Adding_an_Indicator_to_the_Chart"><h2>Adding an Indicator to the Chart</h2></span>Modify the <i>addToChart</i> method so that the chart-checking happens in a new method - <i>checkChart</i>:<br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> addToChart<span style="color: #009900;">&#40;</span>IChart chart<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>checkChart<span style="color: #009900;">&#40;</span>chart<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /><br />&nbsp; &nbsp; chart.<span style="color: #006633;">addIndicator</span><span style="color: #009900;">&#40;</span>indicators.<span style="color: #006633;">getIndicator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SMA&quot;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>smaTimePeriod<span style="color: #009900;">&#125;</span>,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Color</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #003399;">Color</span>.<span style="color: #006633;">BLUE</span><span style="color: #009900;">&#125;</span>, <span style="color: #000000; font-weight: bold;">new</span> DrawingStyle<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>DrawingStyle.<span style="color: #006633;">LINE</span><span style="color: #009900;">&#125;</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>addOHLC<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; IOhlcChartObject ohlc <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>IChartObject obj <span style="color: #339933;">:</span> chart.<span style="color: #006633;">getAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>obj <span style="color: #000000; font-weight: bold;">instanceof</span> IOhlcChartObject<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ohlc <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>IOhlcChartObject<span style="color: #009900;">&#41;</span> obj<span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ohlc <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ohlc <span style="color: #339933;">=</span> chart.<span style="color: #006633;">getChartObjectFactory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">createOhlcInformer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ohlc.<span style="color: #006633;">setPreferredSize</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Dimension</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">100</span>, <span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chart.<span style="color: #006633;">addToMainChart</span><span style="color: #009900;">&#40;</span>ohlc<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//show the ohlc index</span><br />&nbsp; &nbsp; &nbsp; &nbsp; ohlc.<span style="color: #006633;">setShowIndicatorInfo</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span><br /><span style="color: #009900;">&#125;</span><span style="color: #666666; font-style: italic;">//end of addToChart method</span></div>The method to check a chart:<br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> checkChart<span style="color: #009900;">&#40;</span>IChart chart<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>chart <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; printMeError<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;chart for &quot;</span> <span style="color: #339933;">+</span> myInstrument <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; not opened!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>chart.<span style="color: #006633;">getSelectedOfferSide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">myOfferSide</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; printMeError<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;chart offer side is not &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">myOfferSide</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>chart.<span style="color: #006633;">getSelectedPeriod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">myPeriod</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; printMeError<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;chart period is not &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">myPeriod</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>chart.<span style="color: #006633;">getFilter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">filter</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; printMeError<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;chart filter is not &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">filter</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span><br /><span style="color: #009900;">&#125;</span></div><span id="Strategy_Tutorial/Plotting_a_Break-even_Triangle_on_the_Chart"><h2>Plotting a Break-even Triangle on the Chart</h2></span>Implement the <i>addBreakToChart</i> method which is invoked from <i>onTick</i> method to show a visual representation of <i>SL</i> changes in the chart. <br/>In this method we are adding a triangle to our chart if the <i>SL</i> value is changed for an order. Green triangle represents the <i>SL</i> changes for long orders and red one for short orders. The triangle is drawn so that it starts when the order is created and ends when the <i>SL</i> value is changed. We add a text to the triangle to represent the old and new values of <i>SL</i>. <br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> addBreakToChart<span style="color: #009900;">&#40;</span>IOrder changedOrder, ITick tick, <span style="color: #000066; font-weight: bold;">double</span> oldSL, <span style="color: #000066; font-weight: bold;">double</span> newSL<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>openedChart <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /><br />&nbsp; &nbsp; ITriangleChartObject orderSLTriangle <span style="color: #339933;">=</span> factory.<span style="color: #006633;">createTriangle</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Triangle &quot;</span> <span style="color: #339933;">+</span> shorLineCounter<span style="color: #339933;">++</span>, <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; changedOrder.<span style="color: #006633;">getFillTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, changedOrder.<span style="color: #006633;">getOpenPrice</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, tick.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, oldSL, tick.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, newSL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /><br />&nbsp; &nbsp; <span style="color: #003399;">Color</span> lineColor <span style="color: #339933;">=</span> oldSL <span style="color: #339933;">&gt;</span> newSL <span style="color: #339933;">?</span> <span style="color: #003399;">Color</span>.<span style="color: #006633;">RED</span> <span style="color: #339933;">:</span> <span style="color: #003399;">Color</span>.<span style="color: #006633;">GREEN</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; orderSLTriangle.<span style="color: #006633;">setColor</span><span style="color: #009900;">&#40;</span>lineColor<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; orderSLTriangle.<span style="color: #006633;">setLineStyle</span><span style="color: #009900;">&#40;</span>LineStyle.<span style="color: #006633;">SOLID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; orderSLTriangle.<span style="color: #006633;">setLineWidth</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; orderSLTriangle.<span style="color: #006633;">setStickToCandleTimeEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; openedChart.<span style="color: #006633;">addToMainChart</span><span style="color: #009900;">&#40;</span>orderSLTriangle<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /><br />&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//drawing text</span><br />&nbsp; &nbsp; <span style="color: #003399;">String</span> breakTextOldSL <span style="color: #339933;">=</span> <span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; Old SL: %.5f&quot;</span>, oldSL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #003399;">String</span> breakTextNewSL <span style="color: #339933;">=</span> <span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; New SL: %.5f&quot;</span>, newSL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> textVerticalPosition <span style="color: #339933;">=</span> oldSL <span style="color: #339933;">&gt;</span> newSL <span style="color: #339933;">?</span> newSL <span style="color: #339933;">-</span> myInstrument.<span style="color: #006633;">getPipValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> newSL <span style="color: #339933;">+</span> myInstrument.<span style="color: #006633;">getPipValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; ITextChartObject textOldSL <span style="color: #339933;">=</span> factory.<span style="color: #006633;">createText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;textKey1&quot;</span> <span style="color: #339933;">+</span> textCounterOldSL<span style="color: #339933;">++</span>, tick.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, oldSL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; ITextChartObject textNewSL <span style="color: #339933;">=</span> factory.<span style="color: #006633;">createText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;textKey2&quot;</span> <span style="color: #339933;">+</span> textCounterNewSL<span style="color: #339933;">++</span>, tick.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, newSL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; textOldSL.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>breakTextOldSL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; textNewSL.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>breakTextNewSL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; textOldSL.<span style="color: #006633;">setStickToCandleTimeEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; textNewSL.<span style="color: #006633;">setStickToCandleTimeEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; openedChart.<span style="color: #006633;">addToMainChart</span><span style="color: #009900;">&#40;</span>textOldSL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; openedChart.<span style="color: #006633;">addToMainChart</span><span style="color: #009900;">&#40;</span>textNewSL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /><span style="color: #009900;">&#125;</span></div>Here is the complete strategy's java file - <a href="https://www.dukascopy.com/wiki/files//StopLossStrategy.java">StopLossStrategy.java</a>. <br/>Click <a href="#Set_Stop_Loss_price">here</a> to learn more about SL. <br/><hr><span id="Strategy_Tutorial/Testing_the_Strategy"><h2>Testing the Strategy</h2></span>Run the strategy in the <a href="#Historical_Tester"><i>Historical Tester</i></a>. In this example we're using following parameters:<br/><img src="https://www.dukascopy.com/wiki/files//001_StoLossStrategy_set_parameters.png" border="0" /><br/>Consider the output of the chart! Red and green arrows shows the short and long orders in the chart at a time the order is created. Right-side of the triangles shows the <i>SL</i> change time and the new and old <i>SL</i> values. Triangle's left corner starts at the same position in time when the order is created - by doing so one can easy track to which order the <i>SL</i> change is made for. <br/><img src="https://www.dukascopy.com/wiki/files//001_StoLossStrategy_results.png" border="0" /><br/><span id="Strategy_Tutorial/Using_Data_Feed"><h1>Using Data Feed</h1></span>In this section we are going to change the feed data type from 10 minutes to 2-pip renko bars (and alternatively 30 second custom period bars) by leaving the remaining strategy logic the same. In order to create a strategy which <a href="#Chart_feeds">works with different data feeds</a>, one needs to create a class that implements a <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/feed/IFeedListener.html"><i>IFeedListener</i></a> interface. This class needs to implement only one method - <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/feed/IFeedListener.html#onFeedData(com.dukascopy.api.feed.IFeedDescriptor,%20com.dukascopy.api.ITimedData)"><i>IFeedListener.onFeedData</i></a>. This method is invoked every time the feed data arrives. In this example we are going to modify the previously created strategy java file - <a href="https://www.dukascopy.com/wiki/files//StopLossStrategy.java">StopLossStrategy.java</a>. <br/><span id="Strategy_Tutorial/Declaration_of_a_Feed_Type"><h2>Declaration of a Feed Type </h2></span>Modify the <a href="https://www.dukascopy.com/wiki/files//StopLossStrategy.java">StopLossStrategy.java</a> file and add following imports:<br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.IIndicators.AppliedPrice</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.feed.IFeedDescriptor</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.feed.IFeedListener</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.feed.util.RenkoFeedDescriptor</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.feed.util.TimePeriodAggregationFeedDescriptor</span><span style="color: #339933;">;</span></div>As mentioned in the introduction, we need to implement the <a href=http://www.dukascopy.com/client/javadoc/com/dukascopy/api/feed/IFeedListener.html"><i>IFeedListener</i></a> interface to retrieve feed data. We will implement the interface in the same class where the <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IStrategy.html"><i>IStrategy</i></a> interface is implemented:<br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Feeds <span style="color: #000000; font-weight: bold;">implements</span> IStrategy, IFeedListener <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//class' implementation &nbsp; </span><br /><span style="color: #009900;">&#125;</span></div>If we are using more than one data-feed type in the code, then we need to use <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/Instrument.html"><i>Instrument</i></a>, <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/OfferSide.html"><i>OfferSide</i></a>, <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/PriceRange.html"><i>PriceRange</i></a>, <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/Period.html"><i>Period</i></a> according to a <a href="#Chart_feeds/Subscribe_to_a_feed">subscribed feed</a>. All of these values can be retrieved from a <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/feed/IFeedDescriptor.html"><i>IFeedDescriptor</i></a> element. Replace these variables in all methods with calls of <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/feed/IFeedDescriptor.html"><i>IFeedDescriptor</i></a> methods which retrieve the same variables according to the feed type. So, remove the <i>myInstrument</i>, <i>myOfferSide</i>, and <i>myPeriod</i> parameters. <br/>In this example we will provide a choice at strategy's startup to choose between two <a href="#Chart_feeds/Feed_types">feed types</a>, so we are add a new parameter.  <br/>We will need to detect a user's choice of feed and subscribe it. Because of this we declare a new enum. This enum has a constructor that assigns a value for a constant's <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/feed/IFeedDescriptor.html"><i>IFeedDescriptor</i></a> variable. By doing so we can later use the enum's (<i>FeedType</i>) constant to get information about a selected data feed (<i>Instrument</i>, <i>OfferSide</i>, etc.). <br/><div class="dw_article_geshi_code">@Configurable<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Feed type&quot;</span><span style="color: #009900;">&#41;</span><br /><span style="color: #000000; font-weight: bold;">public</span> FeedType myFeed <span style="color: #339933;">=</span> FeedType.<span style="color: #006633;">RENKO_2_PIPS_EURGBP_BID</span><span style="color: #339933;">;</span><br /><br /><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">enum</span> FeedType <span style="color: #009900;">&#123;</span><br /><br />&nbsp; &nbsp; RENKO_2_PIPS_EURGBP_BID <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> RenkoFeedDescriptor<span style="color: #009900;">&#40;</span><span style="color: #003399;">Instrument</span>.<span style="color: #006633;">EURGBP</span>, PriceRange.<span style="color: #006633;">TWO_PIPS</span>, OfferSide.<span style="color: #006633;">BID</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>, <br />&nbsp; &nbsp; TIME_BAR_30_SEC_EURGBP_BID <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> TimePeriodAggregationFeedDescriptor<span style="color: #009900;">&#40;</span><span style="color: #003399;">Instrument</span>.<span style="color: #006633;">EURGBP</span>, Period.<span style="color: #006633;">THIRTY_SECS</span>, <br />&nbsp; &nbsp; &nbsp; &nbsp; OfferSide.<span style="color: #006633;">BID</span>, Filter.<span style="color: #006633;">WEEKENDS</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> IFeedDescriptor feedDescriptor<span style="color: #339933;">;</span><br /><br />&nbsp; &nbsp; FeedType<span style="color: #009900;">&#40;</span>IFeedDescriptor feedDescriptor<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">feedDescriptor</span> <span style="color: #339933;">=</span> feedDescriptor<span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> IFeedDescriptor getFeedDescriptor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> feedDescriptor<span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp; &nbsp; &nbsp; <br /><span style="color: #009900;">&#125;</span></div><span id="Strategy_Tutorial/Subscription_to_a_Feed"><h2>Subscription to a Feed</h2></span>We <a href="#Chart_feeds/Subscribe_to_a_feed">subscribe to a specific type of feed</a> in the <i>onStart</i> method. We add the code that subscribes the feed:<br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onStart<span style="color: #009900;">&#40;</span>IContext context<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">engine</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getEngine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">console</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getConsole</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">history</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getHistory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">context</span> <span style="color: #339933;">=</span> context<span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">indicators</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getIndicators</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">userInterface</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getUserInterface</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">openedChart</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getChart</span><span style="color: #009900;">&#40;</span>myFeed.<span style="color: #006633;">getFeedDescriptor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getInstrument</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">factory</span> <span style="color: #339933;">=</span> openedChart.<span style="color: #006633;">getChartObjectFactory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /><br />&nbsp; &nbsp; Set<span style="color: #339933;">&lt;</span>Instrument<span style="color: #339933;">&gt;</span> instruments <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashSet<span style="color: #339933;">&lt;</span>Instrument<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; instruments.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>myFeed.<span style="color: #006633;">getFeedDescriptor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getInstrument</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; context.<span style="color: #006633;">setSubscribedInstruments</span><span style="color: #009900;">&#40;</span>instruments, <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>drawSMA<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>addToChart<span style="color: #009900;">&#40;</span>openedChart<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printMeError<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Indicators did not get plotted on chart. Check the chart values!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Subscribe to a feed:</span><br />&nbsp; &nbsp; context.<span style="color: #006633;">subscribeToFeed</span><span style="color: #009900;">&#40;</span>myFeed.<span style="color: #006633;">getFeedDescriptor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /><span style="color: #009900;">&#125;</span></div><span id="Strategy_Tutorial/Implementation_of_a_IFeedListener_Interface"><h2>Implementation of a IFeedListener Interface</h2></span>We are moving all logic from <i>onBar</i> to <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/feed/IFeedListener.html#onFeedData(com.dukascopy.api.feed.IFeedDescriptor,%20com.dukascopy.api.ITimedData)"><i>onFeedData</i></a> method. We are no more interested in to execute the <i>onBar</i> method since we are <a href="#Chart_feeds/Use_multiple_feeds">using data feed</a>. We are interested in that our logic is executed every time a new data arrives. To retrieve the data we need to implement <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/feed/IFeedListener.html"><i>IFeedListener</i></a> interface. This interface declares only one method - <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/feed/IFeedListener.html#onFeedData(com.dukascopy.api.feed.IFeedDescriptor,%20com.dukascopy.api.ITimedData)"><i>onFeedData</i></a>. One difference from the code of previously created strategy is that the <i>Instrument</i> and <i>OfferSide</i> values are retrieved from <i>IFeedDescriptor</i> object. All of the code is inserted in a try-catch block, because some of the methods (e.g. <i>IIndicators.calculateIndicator</i>) trow <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/JFException.html"><i>JFException</i></a>. The <i>IBar</i> object is retrieved by casting down <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/ITimedData.html"><i>ITimedData</i></a> object (<i>IBar</i> interface extends <i>ITimedData</i> interface). A feed of indicator is also retrieved in a little bit different manner - we are using <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IIndicators.html#calculateIndicator(com.dukascopy.api.feed.IFeedDescriptor,%20com.dukascopy.api.OfferSide[],%20java.lang.String,%20com.dukascopy.api.IIndicators.AppliedPrice[],%20java.lang.Object[],%20int,%20long,%20int)"><i>IIndicators.calculateIndicator</i></a> method instead of <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/IIndicators.html#sma(com.dukascopy.api.Instrument,%20com.dukascopy.api.Period,%20com.dukascopy.api.OfferSide,%20com.dukascopy.api.IIndicators.AppliedPrice,%20int,%20com.dukascopy.api.Filter,%20int,%20long,%20int)"><i>IIndicators.sma</i></a> because of the usage of <a href="http://www.dukascopy.com/client/javadoc/com/dukascopy/api/feed/IFeedDescriptor.html"><i>IFeedDescriptor</i></a> object.<br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onFeedData<span style="color: #009900;">&#40;</span>IFeedDescriptor feedDescriptor, ITimedData feedData<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br /><br />&nbsp; &nbsp; <span style="color: #003399;">Instrument</span> myInstrument <span style="color: #339933;">=</span> feedDescriptor.<span style="color: #006633;">getInstrument</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; OfferSide myOfferSide <span style="color: #339933;">=</span> feedDescriptor.<span style="color: #006633;">getOfferSide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>feedData <span style="color: #000000; font-weight: bold;">instanceof</span> IBar<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printMeError<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cannot work with tick feed data&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /><br />&nbsp; &nbsp; &nbsp; &nbsp; IBar bar <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>IBar<span style="color: #009900;">&#41;</span> feedData<span style="color: #339933;">;</span><br /><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> candlesBefore <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span>, candlesAfter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">long</span> completedBarTimeL <span style="color: #339933;">=</span> bar.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> smaObjectsFeed <span style="color: #339933;">=</span> indicators.<span style="color: #006633;">calculateIndicator</span><span style="color: #009900;">&#40;</span>feedDescriptor, <span style="color: #000000; font-weight: bold;">new</span> OfferSide<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> myOfferSide <span style="color: #009900;">&#125;</span>, <span style="color: #0000ff;">&quot;SMA&quot;</span>,<br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> AppliedPrice<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> AppliedPrice.<span style="color: #006633;">CLOSE</span> <span style="color: #009900;">&#125;</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> smaTimePeriod <span style="color: #009900;">&#125;</span>, candlesBefore, feedData.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, candlesAfter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> sma <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> smaObjectsFeed<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// sma has just 1 output &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><br /><br />&nbsp; &nbsp; &nbsp; &nbsp; printMe<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bar SMA Values: Second-to-last = %.5f; Last Completed = %.5f&quot;</span>, sma<span style="color: #009900;">&#91;</span>SECOND_TO_LAST<span style="color: #009900;">&#93;</span>, sma<span style="color: #009900;">&#91;</span>PREV<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /><br />&nbsp; &nbsp; &nbsp; &nbsp; IEngine.<span style="color: #006633;">OrderCommand</span> myCommand <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; printMe<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bar SMA Values: Second-to-last = %.5f; Last Completed = %.5f&quot;</span>, sma<span style="color: #009900;">&#91;</span>SECOND_TO_LAST<span style="color: #009900;">&#93;</span>, sma<span style="color: #009900;">&#91;</span>PREV<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>sma<span style="color: #009900;">&#91;</span>PREV<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> sma<span style="color: #009900;">&#91;</span>SECOND_TO_LAST<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printMe<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SMA in up-trend&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//indicator goes up</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myCommand <span style="color: #339933;">=</span> IEngine.<span style="color: #006633;">OrderCommand</span>.<span style="color: #006633;">BUY</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentSMADirection <span style="color: #339933;">=</span> SMATrend.<span style="color: #006633;">UP</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>sma<span style="color: #009900;">&#91;</span>PREV<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;</span> sma<span style="color: #009900;">&#91;</span>SECOND_TO_LAST<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printMe<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SMA in down-trend&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//indicator goes down</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myCommand <span style="color: #339933;">=</span> IEngine.<span style="color: #006633;">OrderCommand</span>.<span style="color: #006633;">SELL</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentSMADirection <span style="color: #339933;">=</span> SMATrend.<span style="color: #006633;">DOWN</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> lastTickBid <span style="color: #339933;">=</span> history.<span style="color: #006633;">getLastTick</span><span style="color: #009900;">&#40;</span>myInstrument<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getBid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> lastTickAsk <span style="color: #339933;">=</span> history.<span style="color: #006633;">getLastTick</span><span style="color: #009900;">&#40;</span>myInstrument<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getAsk</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> stopLossValueForLong <span style="color: #339933;">=</span> myInstrument.<span style="color: #006633;">getPipValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> stopLossPips<span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> stopLossValueForShort <span style="color: #339933;">=</span> myInstrument.<span style="color: #006633;">getPipValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> takeProfitPips<span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> stopLossPrice <span style="color: #339933;">=</span> myCommand.<span style="color: #006633;">isLong</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #009900;">&#40;</span>lastTickBid <span style="color: #339933;">-</span> stopLossValueForLong<span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>lastTickAsk <span style="color: #339933;">+</span> stopLossValueForLong<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> takeProfitPrice <span style="color: #339933;">=</span> myCommand.<span style="color: #006633;">isLong</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #009900;">&#40;</span>lastTickBid <span style="color: #339933;">+</span> stopLossValueForShort<span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>lastTickAsk <span style="color: #339933;">-</span> stopLossValueForShort<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//if SMA trend direction is changed, then create a new order</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>currentSMADirection <span style="color: #339933;">!=</span> previousSMADirection<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; previousSMADirection <span style="color: #339933;">=</span> currentSMADirection<span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IOrder newOrder <span style="color: #339933;">=</span> engine.<span style="color: #006633;">submitOrder</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MyStrategyOrder&quot;</span> <span style="color: #339933;">+</span> uniqueOrderCounter<span style="color: #339933;">++</span>, myInstrument, myCommand, <span style="color: #cc66cc;">0.1</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1</span>, stopLossPrice, takeProfitPrice<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; createdOrderMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>newOrder, <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>openedChart <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//get current time of a bar from IFeedDescriptor object</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">long</span> time <span style="color: #339933;">=</span> history.<span style="color: #006633;">getFeedData</span><span style="color: #009900;">&#40;</span>feedDescriptor, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//draw the &nbsp;ISignalDownChartObject in the current bar</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> space <span style="color: #339933;">=</span> myInstrument.<span style="color: #006633;">getPipValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//space up or down from bar for ISignalDownChartObject</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IChartDependentChartObject signal <span style="color: #339933;">=</span> myCommand.<span style="color: #006633;">isLong</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">?</span> factory.<span style="color: #006633;">createSignalUp</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;signalUpKey&quot;</span> <span style="color: #339933;">+</span> signals<span style="color: #339933;">++</span>, time, bar.<span style="color: #006633;">getLow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> space<span style="color: #009900;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">:</span> factory.<span style="color: #006633;">createSignalDown</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;signalDownKey&quot;</span> <span style="color: #339933;">+</span> signals<span style="color: #339933;">++</span>, time, bar.<span style="color: #006633;">getHigh</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> space<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; signal.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MyStrategyOrder&quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>uniqueOrderCounter <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; openedChart.<span style="color: #006633;">addToMainChart</span><span style="color: #009900;">&#40;</span>signal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /><span style="color: #009900;">&#125;</span></div>The <i>onBar</i> method remains with empty body:<br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onBar<span style="color: #009900;">&#40;</span><span style="color: #003399;">Instrument</span> instrument, Period period, IBar askBar, IBar bidBar<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span></div><span id="Strategy_Tutorial/Modification_of_the_Chart-checking_Method"><h2>Modification of the Chart-checking Method</h2></span>In the chart-checking method we replace the manner of getting <i>Instrument</i> and <i>OfferSide</i> objects. We are using <i>IFeedDescriptor</i> object to retrieve them. The logic of checking the chart is also a little bit different from the previous one. We need to detect the type of data-feed and verify the chart according to it:<br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> checkChart<span style="color: #009900;">&#40;</span>IChart chart<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>chart <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; printMeError<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;chart for &quot;</span> <span style="color: #339933;">+</span> myFeed.<span style="color: #006633;">getFeedDescriptor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getInstrument</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; not opened!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>chart.<span style="color: #006633;">getSelectedOfferSide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> myFeed.<span style="color: #006633;">getFeedDescriptor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getOfferSide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; printMeError<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;chart offer side is not &quot;</span> <span style="color: #339933;">+</span> myFeed.<span style="color: #006633;">getFeedDescriptor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getOfferSide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>chart.<span style="color: #006633;">getFeedDescriptor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getDataType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> DataType.<span style="color: #006633;">RENKO</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>chart.<span style="color: #006633;">getPriceRange</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> myFeed.<span style="color: #006633;">getFeedDescriptor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getPriceRange</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printMeError<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;chart price range is not &quot;</span> <span style="color: #339933;">+</span> myFeed.<span style="color: #006633;">getFeedDescriptor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getPriceRange</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>chart.<span style="color: #006633;">getFeedDescriptor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getDataType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> DataType.<span style="color: #006633;">TIME_PERIOD_AGGREGATION</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>chart.<span style="color: #006633;">getSelectedPeriod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> myFeed.<span style="color: #006633;">getFeedDescriptor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getPeriod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printMeError<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;chart period is not &quot;</span> <span style="color: #339933;">+</span> myFeed.<span style="color: #006633;">getFeedDescriptor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getPeriod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp;<br /><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>chart.<span style="color: #006633;">getFilter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">filter</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; printMeError<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;chart filter is not &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">filter</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span><br /><span style="color: #009900;">&#125;</span></div>The complete source code of the strategy - <a href="https://www.dukascopy.com/wiki/files//Feeds.java">Feeds.java</a>. <br/><hr><span id="Strategy_Tutorial/Testing_the_Strategy"><h2>Testing the Strategy</h2></span>In this example we are using more specific time (30 seconds) and price (2 pips) ranges. Before launching the strategy one needs to open a chart with the same parameters as given in the strategy parameters. To add specific ranges of time/price ranges one needs to choose <i>Tools -> Preferences -> Period</i> and add required periods. To launch the strategy you need to add <i>Renko 2 Pips</i> and <i>30 Seconds</i> periods.<br/><span id="Strategy_Tutorial/Testing_with_a_Renko_Feed"><h3>Testing with a Renko Feed</h3></span>We are running the test of <i>Renko 2 Pips</i> with following parameters:<br/><img src="https://www.dukascopy.com/wiki/files//001_Feeds_Renko_Params.png" border="0" /><br/>Here is an example picture of results of <i>Renko</i> (2 pips) feed type. We can see that the <i>SMA</i> indicator and long/short orders are added to the chart:<br/><img src="https://www.dukascopy.com/wiki/files//001_Feeds_RenkoResults.png" border="0" /><br/><span id="Strategy_Tutorial/Testing_with_a_Custom_Period_Candle_Feed"><h3>Testing with a Custom Period Candle Feed</h3></span>When testing <i>Time Bar 30 second</i> feed, we are using smaller <i>Stop Loss</i> and <i>Break Event</i> pips because with the larger values it would be hard to track the <i>SL</i> changes (plotted triangles would be very wide). Used parameters:<br/><img src="https://www.dukascopy.com/wiki/files//001_Feeds_30Sec_Params.png" border="0" /><br/>Results:<br/><img src="https://www.dukascopy.com/wiki/files//001_Feeds_30SecResults.png" border="0" /><br/><span id="Strategy_Tutorial/Adding_GUI_to_Strategy"><h1>Adding <i>GUI</i> to Strategy</h1></span>In this example we are adding a <i>GUI</i> (Graphical User Interface) element (<a href="http://docs.oracle.com/javase/6/docs/api/javax/swing/JDialog.html"><i>JDialog</i></a>) which will show up a warning dialog every time a new order is submitted. The dialog will contain the message text with a label of the created order. If it happens that the new order is created before the old dialog is closed, then the dialog simply changes the new order's label in the message text. <br/>We will modify our previously created strategy - <a href="https://www.dukascopy.com/wiki/files//Feeds.java">Feeds.java</a>. <br/><br/>At first, we're adding new imports for <i>GUI</i>. We are going to use a <a href="http://docs.oracle.com/javase/6/docs/api/javax/swing/JDialog.html"><i>JDialog</i></a> object, that will include (wraps) <a href="http://docs.oracle.com/javase/6/docs/api/javax/swing/JOptionPane.html"><i>JOptionPane</i></a> (the content of <i>JDialog</i>) object. <br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.event.ComponentAdapter</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.event.ComponentEvent</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.swing.JDialog</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.swing.JOptionPane</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.swing.SwingUtilities</span><span style="color: #339933;">;</span></div>Declare instance variables for the dialog.<br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> dialogClosed <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">JOptionPane</span> optionPane <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">JOptionPane</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #003399;">JOptionPane</span>.<span style="color: #006633;">WARNING_MESSAGE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">JDialog</span> dialog <span style="color: #339933;">=</span> optionPane.<span style="color: #006633;">createDialog</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div>By default the dialog would block an access to the platform while the dialog is in open state. To make the dialog not modal, add the following line of code to the <i>onStart</i> method:<br/><div class="dw_article_geshi_code">dialog.<span style="color: #006633;">setModal</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div>To show a dialog we call a <i>showNotification</i> method from <i>onDataFeed</i> method. Call for the <i>showNotification</i> method if a new order is created. Here is a code snippet:<br/><div class="dw_article_geshi_code"><span style="color: #666666; font-style: italic;">// ..</span><br /><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>currentSMADirection <span style="color: #339933;">!=</span> previousSMADirection<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; previousSMADirection <span style="color: #339933;">=</span> currentSMADirection<span style="color: #339933;">;</span><br />&nbsp; &nbsp; IOrder newOrder <span style="color: #339933;">=</span> engine.<span style="color: #006633;">submitOrder</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MyStrategyOrder&quot;</span> <span style="color: #339933;">+</span> uniqueOrderCounter<span style="color: #339933;">++</span>, myInstrument, myCommand, <span style="color: #cc66cc;">0.1</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1</span>, stopLossPrice, takeProfitPrice<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; createdOrderMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>newOrder, <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /><br />&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//invoke the method that will show up a dialog</span><br />&nbsp; &nbsp; <span style="color: #003399;">SwingUtilities</span>.<span style="color: #006633;">invokeLater</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Runnable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; showNotification<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /><span style="color: #666666; font-style: italic;">//..</span></div>The <i>showNotification</i> method shows the dialog every time a new order is created (if the old dialog is closed) or modifies the message of an existing dialog (if the old dialog is not closed).<br/>If the dialog is not closed, then the method adds a logic which listens to dialog's events. In this case we are listening for window's close events. If the window of dialog gets closed then the <i>dialogClosed</i> variable is set to false, so at the next method's execution we will now that the dialog is in closed position. <br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> showNotification<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp; &nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; <span style="color: #003399;">String</span> message <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Order MyStrategyOrder&quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>uniqueOrderCounter <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; created.&quot;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp;<br /><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>dialogClosed<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp; dialog.<span style="color: #006633;">addComponentListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ComponentAdapter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> componentHidden<span style="color: #009900;">&#40;</span><span style="color: #003399;">ComponentEvent</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dialogClosed <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; optionPane.<span style="color: #006633;">setMessage</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; dialog.<span style="color: #006633;">setVisible</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; dialogClosed <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; optionPane.<span style="color: #006633;">setMessage</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp; dialog.<span style="color: #006633;">validate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp;<br /><span style="color: #009900;">&#125;</span></div>Here is the completed strategy's java file - <a href="https://www.dukascopy.com/wiki/files//FeedsGUI.java">FeedsGUI.java</a>. <br/><hr><br/>When running the strategy, a new warning dialog appears. If we close the dialog, then at the next order submission a new dialog appears. If we don't close the dialog before the new order is submitted, then the message of the dialog is changed. The dialog should look like the following one:<br/><img src="https://www.dukascopy.com/wiki/files//001_FeedsGUI_Dialog.png" border="0" /><br/>One can click <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html">here</a> to learn more of how to use <a href="http://docs.oracle.com/javase/6/docs/api/javax/swing/JDialog.html"><i>JDialog</i></a> and other Java&trade; <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/index.html">swing</a> objects. <br/>